<?php

namespace app\qsapi\model;

use think\cache\driver\Redis;

class BoxActiveLog extends BaseModel
{
    // 直接使用配置参数名; 数据库连接配置;用与连接哪个个数据库
    protected $connection = 'Db_log';

    /**
     * Desc : kafka 盒子激活日志消费
     * User : Steven
     * Date : 2024-06-18 17:20
     * @param $data
     * @return int|string
     */
    public function createBoxActiveLog($data)
    {
        $device = $data['device'] ?? '';
        $newDevice  = (new BoxActiveLog())
            ->where(['device' => $device])
            ->field('id,first_act_time,ctime')
            ->order('id', 'asc')
            ->find();
        if (!empty($newDevice['id'])) {
            //重复激活
            $data['status']         = 2;
            $data['first_act_time'] = $newDevice['ctime'];
        } else {
            $data['first_act_time'] = date('Y-m-d H:i:s');
        }
        return $this->insertGetId($data);
    }


    /**
     * Desc : 将接入的广告数据写入广告的redis
     * User : steven
     * Date : 2024-03-05 09:03
     * event_type : 1:激活 2:注册 3:下单
     */
    public function reportAdRedis($postData = [], $event_type = 1, $clientData = []): int
    {
        $os_model   = $postData['os_model'] ?? '';
        $imei       = $postData['imei'] ?? '';
        $oaid       = $postData['oaid'] ?? '';
        $idfa       = $postData['idfa'] ?? '';
        $plat       = $postData['platform'] ?? 0;
        $adid       = $postData['promoteid'] ?? 0;
        $ip         = $clientData['ip'] ?? '';
        $os_version = $clientData['os_version'] ?? '';
        if (!empty($imei)) {
            $imei_md5 = is_md5($imei) ? $imei : md5($imei);
        }
        if (!empty($oaid)) {
            $oaid_md5 = is_md5($oaid) ? $oaid : md5($oaid);
        }
        if (!empty($idfa)) {
            $idfa_md5 = is_md5($idfa) ? $idfa : md5($idfa);
        }

        if ($plat == 1) {
            $os = 1;
        } elseif ($plat == 2) {
            $os = 2;
        }elseif ($plat == 3){
            $os = 3;
        }

        $redisData = [
            'member_id'            => $postData['member_id'] ?? '',
            'os'                   => $os ?? 0,
            'androidid'            => $postData['android_id'] ?? '',
            'imei'                 => $imei,
            'imei_md5'             => $imei_md5 ?? '', //imei md5'
            'oaid'                 => $oaid,
            'oaid_md5'             => $oaid_md5 ?? '', //oaid md5'
            'idfa'                 => $idfa,
            'idfa_md5'             => $idfa_md5 ?? '', //idfa md5'
            'ip'                   => $ip,
            'ip_model_version_md5' => md5($ip . $os_version) ?? '',
            'os_model'             => $os_model,
            'os_version'           => $os_version, //获取手机系统版本
            'mac'                  => $postData['mac'] ?? '',
            'event_type'           => $event_type, //上报类型
            'active_log_id'        => $postData['active_id'] ?? 0, //激活插入日志表中的ID
            'ad_id'                => $adid, //广告ID
        ];
        $redis     = new Redis(getRedisConfig());
        $redis_id  = $redis->lPush('QIANNIAN_BOX_AD_ATTRIBUTION', json_encode($redisData));
        return $redis_id ?? 0;
    }
}